package com.tools.common.rest;

import android.util.Log;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class ApiWrapper {
    public static <T> void call(Observable<T> observable, final Callback cb) {

        if(cb != null) cb.onStart();

        observable
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<T>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(T value) {
                if(cb != null) cb.onSuccess(value);
            }

            @Override
            public void onError(final Throwable e) {
                e.printStackTrace();
                if(cb != null) cb.onFailure(e);
            }

            @Override
            public void onComplete() {
                Log.d("test", "test complete");
                if(cb != null) cb.onEnd();
            }
        });
    }
}
